<?php
namespace Custom\Model;
use Think\Model;
//订单
class OrderModel extends Model {

    /**
     * 订单首页显示model
     */
    public function getAllOrder($page_num,$where){
        $field = 'a.order_number,a.pay_status,a.time,a.pay_money,a.id,b.cart,b.user_name,b.table_number,c.nick_name';
        $data = M('order_detail')->where($where)->join('as b left join sp_order as a on b.order_number=a.order_number left join sp_user as c on c.user_id=b.user_id')->order('a.time desc')->field($field)->limit($page_num->firstRow.','.$page_num->listRows)->select();
        $status = array(1=>'待支付',2=>'正在送餐中',3=>'待收货',4=>'已完成',5=>'已取消');
        foreach ($data as $key => &$value) {
            $value['date'] = date('Y-m-d H:i',$value['time']);
            $value['status'] = $status[$value['order_status']];
            if($value['cart']){
                $cart = array();
                $cart = json_decode($value['cart'],true);
                $productID = array();
                foreach ($cart as $id => $num) {
                    $productID[] = $id;
                }
                $productID = implode(',',$productID);
                $product = array();
                $sql = "
                select * from sp_goods_spec_option  as a
                LEFT JOIN sp_goods as b ON a.goods_id=b.goods_id
                where a.goods_option_id in($productID)";
                $product = $this->query($sql);
                foreach ($product as $k => &$v) {
                    $v['price'] = $cart[$v['goods_option_id']] * $v['price'];
                    $v['product_num'] = $cart[$v['goods_option_id']];
                    if(empty($v['option_title'])){
                        $value['goods_option_name'] .= $v['goods_name'].';';
                    }else{
                        $value['goods_option_name'] .= $v['goods_name'].'('.$v['option_title'].');';
                    }
                }
            }

        }
        return $data;
    }
    /*
     * 订单详情页model
     */
    public function getOrderDetail($where){
        $field = 'a.order_number,a.pay_status,a.time,a.pay_money,a.id oid,b.cart,b.user_name,b.mobile,b.address,b.id,b.table_number,c.nick_name';
        $data = M('order')->where($where)->join('as a left join sp_order_detail as b on a.order_number=b.order_number left join sp_user as c on b.user_id=c.user_id')->field($field)->select();
        $status = array(1=>'待支付',2=>'正在送餐中',3=>'待收货',4=>'已完成',5=>'已取消');
        foreach ($data as $key => &$value) {
            $value['date'] = date('Y-m-d H:i',$value['time']);
            $value['status'] = $status[$value['order_status']];
            $cart = array();
            $cart = json_decode($value['cart'],true);
            $productID = array();
            foreach ($cart as $id => $num) {
                $productID[] = $id;
            }
            $productID = implode(',',$productID);
            $product = array();
            $sql = "
                select * from sp_goods_spec_option  as a
                LEFT JOIN sp_goods as b ON a.goods_id=b.goods_id
                where a.goods_option_id in($productID)";
            $product = $this->query($sql);
            foreach ($product as $k => &$v) {
                $v['price'] = $cart[$v['goods_option_id']] * $v['price'];
                $v['product_num'] = $cart[$v['goods_option_id']];
                if(empty($v['option_title'])){
                    $value['goods_option_name'] .= $v['goods_name'].';';
                }else{
                    $value['goods_option_name'] .= $v['goods_name'].'('.$v['option_title'].');';
                }
                $value['goods_id'] .= $v['goods_id'];
            }
        }
        return $value;
    }
}
